package com.mew.chess.core.game.dyj;

import cn.hutool.core.date.DateUnit;
import com.mew.chess.core.common.handler.AbstractEnterExitHandler;
import com.mew.chess.core.common.handler.ChessEEAnnotation;
import com.mew.chess.core.common.message.request.EnterGameRequestMessage;
import com.mew.chess.core.game.dyj.game.message.UserMessage;
import com.mew.chess.core.game.dyj.game.DyjGameManager;
import com.mew.chess.core.game.dyj.game.DyjRoomManager;
import com.mew.chess.core.game.dyj.game.common.DyjConstantValue;
import com.mew.chess.core.game.dyj.game.message.EnterResponseMessage;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.rest.game.ChessEnterRoomResponse;
import com.mew.common.util.DateUtils;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 进入退出游戏Handler
 */
@ChessEEAnnotation("DYJ")
@Service
@Slf4j
public class DyjEEHandler extends AbstractEnterExitHandler {

    /**
     * 坐下 准备进入房间
     */
    @Override
    public ResponseCode doSitDown(int roomId, GameSession chessSession, MewSession session, boolean isRepeat) {
        //1 校验
        DyjRoomManager roomManager = DyjGameManager.getInstance().getController(roomId);
        if (null == roomManager){
            log.error("DyjRoomManager roomId {} is null", roomId);
            return ResponseCode.SysException;
        }
        if (roomManager.getOnlineUser().size() > DyjConstantValue.MAX_USER){
            log.error("DyjRoomManager roomId {} room user is full", roomId);
            return ResponseCode.SysException;
        }
        return ResponseCode.Success;
    }

    /**
     * 进入房间完毕
     */
    @Override
    public void doFinish(GameSession chessSession, ZebraCommand command, ChessEnterRoomResponse enterRoomResponse) {
        DyjRoomManager roomManager = DyjGameManager.getInstance().getController(chessSession.getRoomId());
        UserMessage userVO = roomManager.registerOnlineUser(chessSession);

        //2 当有一人坐下 游戏就开始
        if (!roomManager.checkIsNoStart()){
            roomManager.startGame();
        }

        EnterResponseMessage responseMessage = new EnterResponseMessage();

        responseMessage.setUserMessage(userVO);
        responseMessage.setRoomBetArray(roomManager.getBetArrays());
        responseMessage.setLastBetNumber(enterRoomResponse.getLastBetNumber());

        responseMessage.setLinkType(roomManager.getLinkType());
        responseMessage.setRemainTime(DateUtils.remainTimeMillis(roomManager.getStartTime() == 0 ? System.currentTimeMillis() : roomManager.getStartTime(), roomManager.getRuleTime()));

        responseMessage.setIno(roomManager.getIno());
        responseMessage.setRoomCost(0);
        responseMessage.setTypebetNumber(roomManager.getTypeNumber());
        long[][] bets=new long[userVO.getBetRecord().size()][2];
        List<Integer> keys = userVO.getBetRecord().keySet().stream().collect(Collectors.toList());
        for (int i = 0; i < keys.size(); i++) {
            bets[i][0]=keys.get(i);
            bets[i][1]=userVO.getBetRecord().get(keys.get(i))[0];
        }
        responseMessage.setPlayerTypeBetNumber(bets);
        responseMessage.setDices(roomManager.getDyjResult().getDices());
        responseMessage.setHistory(roomManager.getHistory());
        responseMessage.setBigRList(roomManager.getBigRList().values());
        command.setToken(chessSession.getTk());
        command.sendMessage(responseMessage);

    }

    /**
     * 退出游戏
     * @param roomId
     */
    @Override
    public boolean doExitRoom(MewSession session, int roomId) {
        if(!session.checkChessPlaying()) {
            DyjRoomManager roomManager = DyjGameManager.getInstance().getController(roomId);
            roomManager.exitRoom(session.uid);
            return false;
        }
        return true;
    }
}
